package com.ruoyi.web.controller.fund;

import com.ruoyi.area.domain.HouseAreaExportT;
import com.ruoyi.area.domain.HouseAreaT;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.MD5;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.doorBindQuery.domain.HouseDoorBindQuery;
import com.ruoyi.doorBindQuery.service.IHouseDoorBindQueryService;
import com.ruoyi.group.domain.HouseGroupT;
import com.ruoyi.unit.domain.HouseUnitT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 户绑定Controller
 *
 * @author lijie
 * @date 2025-04-17
 */
@RestController
@RequestMapping("/fund/doorBindQuery")
public class HouseDoorBindQueryController extends BaseController {
    @Autowired
    private IHouseDoorBindQueryService houseDoorBindQueryService;

    /**
     * 查询户绑定列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseDoorBindQuery houseDoorBindQuery) throws Exception {
        startPage();
        if(!("".equals(houseDoorBindQuery.getUserName())|| houseDoorBindQuery.getUserName()==null)){
            houseDoorBindQuery.setUserName(MD5.encrypt(houseDoorBindQuery.getUserName()));
        }
        List<HouseDoorBindQuery> list = houseDoorBindQueryService.selectHouseDoorBindQueryList(houseDoorBindQuery);
        for (HouseDoorBindQuery houseDoorBindQuery1 : list) {
            houseDoorBindQuery1.setUserName(MD5.decrypt(houseDoorBindQuery1.getUserName()));
            houseDoorBindQuery1.setTel(MD5.decrypt(houseDoorBindQuery1.getTel()));
            houseDoorBindQuery1.setPapersCode(MD5.decrypt(houseDoorBindQuery1.getPapersCode()));
        }
        return getDataTable(list);
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<HouseDoorBindQuery> util = new ExcelUtil<HouseDoorBindQuery>(HouseDoorBindQuery.class);
        List<HouseDoorBindQuery> userList = util.importExcel(file.getInputStream());
        houseDoorBindQueryService.importDoorBindInfo(userList);
        return AjaxResult.success(1);
    }

    @GetMapping("/getAreaBindInfo")
    public TableDataInfo getAreaBindInfo(HouseDoorBindQuery houseDoorBindQuery) {
        List<HouseDoorBindQuery> list = houseDoorBindQueryService.getAreaBindInfo(houseDoorBindQuery);
        return getDataTable(list);
    }

    @GetMapping("/getAreaBindInfo1/{areaName}")
    public TableDataInfo getAreaBindInfo1(@PathVariable("areaName") String areaName) {
        HouseDoorBindQuery houseAreaT=new HouseDoorBindQuery();
        houseAreaT.setAreaName(areaName);
        List<HouseDoorBindQuery> list = houseDoorBindQueryService.getAreaBindInfo(houseAreaT);
        return getDataTable(list);
    }

    @GetMapping(value = "/getGroupBindInfo/{areaName}")
    public TableDataInfo getGroupBindInfo(@PathVariable("areaName") String areaName)
    {
        HouseDoorBindQuery houseGroupT = new HouseDoorBindQuery();
        houseGroupT.setAreaName(areaName);
        List<HouseDoorBindQuery> list = houseDoorBindQueryService.getGroupBindInfo(houseGroupT);
        return getDataTable(list);
    }

    @GetMapping(value = "/getUnitBindInfo/{areaName}/{groupName}" )
    public TableDataInfo getUnitBindInfo(@PathVariable("areaName") String areaName,@PathVariable("groupName") String groupName)
    {
        HouseDoorBindQuery houseUnitT= new HouseDoorBindQuery();
        houseUnitT.setAreaName(areaName);
        houseUnitT.setGroupName(groupName);
        List<HouseDoorBindQuery> list = houseDoorBindQueryService.getUnitBindInfo(houseUnitT);
        return getDataTable(list);
    }

    @GetMapping(value = "/getDoorBindInfo/{areaName}/{groupName}/{unitName}")
    public TableDataInfo getDoorBindInfo(@PathVariable("areaName") String areaName, @PathVariable("groupName") String groupName, @PathVariable("unitName") String unitName) {
        HouseDoorBindQuery houseDoorT = new HouseDoorBindQuery();
        houseDoorT.setAreaName(areaName);
        houseDoorT.setGroupName(groupName);
        houseDoorT.setUnitName(unitName);
        List<HouseDoorBindQuery> list = houseDoorBindQueryService.getDoorBindInfo(houseDoorT);
        return getDataTable(list);
    }


    @GetMapping(value = "/getBindInfo/{houseId}")
    public AjaxResult getBindInfo(@PathVariable("houseId") String houseId) throws Exception {
        HouseDoorBindQuery houseDoorT = houseDoorBindQueryService.getBindInfo(houseId);
        houseDoorT.setUserName(MD5.decrypt(houseDoorT.getUserName()));
        houseDoorT.setTel(MD5.decrypt(houseDoorT.getTel()));
        return AjaxResult.success(houseDoorT);
    }
}
